/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipcc.manager.entity;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class IPCC_GROUP_CHANNEL implements Serializable{
    private String id;
    private String group_id;
    private String source_number;
    private String from_date;
    private String to_date;

    public IPCC_GROUP_CHANNEL() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getGroup_id() {
        return group_id;
    }

    public void setGroup_id(String group_id) {
        this.group_id = group_id;
    }

    public String getSource_number() {
        return source_number;
    }

    public void setSource_number(String source_number) {
        this.source_number = source_number;
    }

    public String getFrom_date() {
        return from_date;
    }

    public void setFrom_date(String from_date) {
        this.from_date = from_date;
    }

    public String getTo_date() {
        return to_date;
    }

    public void setTo_date(String to_date) {
        this.to_date = to_date;
    }
    
    public static List<IPCC_GROUP_CHANNEL> getAllBySource_number(String source_number) throws Exception{
        List<IPCC_GROUP_CHANNEL> list = new ArrayList<IPCC_GROUP_CHANNEL>();
        Connection cn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String strSQL = " select id,group_,source_number,to_char(from_date,'dd-mm-yyyy'),to_char(to_date,'dd-mm-yyyy') from ipcc_group_channel where source_number = ?";
        try {
            cn = AppServer.getConnection();
            pst = cn.prepareStatement(strSQL);
            pst.setString(1, source_number);
            
            rs = pst.executeQuery();
            while (rs.next()) {
                IPCC_GROUP_CHANNEL igc = new IPCC_GROUP_CHANNEL();
                igc.setId(rs.getString(1));
                igc.setGroup_id(rs.getString(2));
                igc.setSource_number(rs.getString(3));
                igc.setFrom_date(rs.getString(4));
                igc.setTo_date(rs.getString(5));
                list.add(igc);
            }
            return list;
        } finally {
            Database.closeObject(rs);
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }
    
    public boolean insert() throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;        
        try {
            cn = AppServer.getConnection();            
            setId(Database.getSequenceValue(cn, "seq_channel"));
            String strSQL = "insert into ipcc_group_channel(id,group_,source_number,from_date,to_date) values(?,?,?,to_date(?,'dd-mm-yyyy'),to_date(?,'dd-mm-yyyy'))";                        
            pst = cn.prepareStatement(strSQL);
            pst.setInt(1, Integer.parseInt(getId()));
            pst.setString(2, getGroup_id());
            pst.setString(3, getSource_number());
            pst.setString(4, getFrom_date());
            pst.setString(5, getTo_date());
            
            return pst.executeUpdate() > 0;
        } finally {
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }
    public boolean update() throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;
        String strSQL = "update ipcc_group_channel"
                + " set group_ = ?,source_number = ?,from_date = to_date(?,'dd-mm-yyyy'),to_date = to_date(?,'dd-mm-yyyy')"
                + " where id = ?";
        try {
            cn = AppServer.getConnection();
            pst = cn.prepareStatement(strSQL);            
            pst.setString(1, getGroup_id());
            pst.setString(2, getSource_number());
            pst.setString(3, getFrom_date());
            pst.setString(4, getTo_date());
            pst.setInt(5, Integer.parseInt(getId()));
            
            return pst.executeUpdate() > 0;
        } finally {
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }
    public boolean remove() throws Exception{
        Connection cn = null;
        Statement st = null;
        String strSQL = "delete from ipcc_group_channel where id = "+getId()+"";
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            return st.executeUpdate(strSQL) > 0;            
        } finally {
            Database.closeObject(st);
            Database.closeObject(cn);
        }
    }    
}
